import wx
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure


class PlotFrame(wx.Frame):
    def __init__(self, parent, title):
        super(PlotFrame, self).__init__(parent, title=title, size=(800, 600))

        self.figure = Figure()
        # 在Figure 对象上添加一个子图
        self.axes = self.figure.add_subplot(111)

        x = np.linspace(0, 2 * np.pi, 100)
        y = np.cos(x)

        self.axes.plot(x, y)
        self.axes.set_xlabel('X axis')
        self.axes.set_ylabel('Y axis')

        self.axes.set_title('Sine Wave')

        # 创建一个画布
        self.canvas = FigureCanvas(self, -1, self.figure)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, 1, wx.EXPAND)
        # 将画布添加到 Panel 上,设置窗口大小调整器
        self.SetSizer(sizer)
        # 绑定一个关闭事件
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnClose(self, event):
        self.Destroy()


app = wx.App(False)
frame = PlotFrame(None, "Matplotlib in wxPython")
frame.Show(True)
app.MainLoop()
